* Prepare CTB data
egen subject_id=group(responseid)

drop *_pagesubmit *_clickcount *_lastclick *_firstclick durationinseconds

* Rename ctb variables using and id so that better to work with
* These vars capture which choice respondents
* made in each of the 24 decisions
g ctb1_q1=q35
g ctb1_q2=q36
g ctb1_q3=q37
g ctb1_q4=q38
g ctb1_q5=q39
g ctb1_q6=q310
g ctb1_q7=q312
g ctb1_q8=q313
g ctb1_q9=q314
g ctb1_q10=q315
g ctb1_q11=q316
g ctb1_q12=q317
g ctb1_q13=q319
g ctb1_q14=q320
g ctb1_q15=q321
g ctb1_q16=q322
g ctb1_q17=q323
g ctb1_q18=q324
g ctb1_q19=q326
g ctb1_q20=q327
g ctb1_q21=q328
g ctb1_q22=q329
g ctb1_q23=v198
g ctb1_q24=q331

g ctb2_q1=q511
g ctb2_q2=q512
g ctb2_q3=q513
g ctb2_q4=q514
g ctb2_q5=q515
g ctb2_q6=q516
g ctb2_q7=q518
g ctb2_q8=q519
g ctb2_q9=q520
g ctb2_q10=q521
g ctb2_q11=q522
g ctb2_q12=q523
g ctb2_q13=q525
g ctb2_q14=q526
g ctb2_q15=q527
g ctb2_q16=q528
g ctb2_q17=q529
g ctb2_q18=q530
g ctb2_q19=q532
g ctb2_q20=q533
g ctb2_q21=q534
g ctb2_q22=q535
g ctb2_q23=q536
g ctb2_q24=q537

g ctb3_q1=q542
g ctb3_q2=q543
g ctb3_q3=q544
g ctb3_q4=q545
g ctb3_q5=q546
g ctb3_q6=q547
g ctb3_q7=q549
g ctb3_q8=q550
g ctb3_q9=q551
g ctb3_q10=q552
g ctb3_q11=q553
g ctb3_q12=q554
g ctb3_q13=q556
g ctb3_q14=q557
g ctb3_q15=q558
g ctb3_q16=q559
g ctb3_q17=q560
g ctb3_q18=q561
g ctb3_q19=q563
g ctb3_q20=q564
g ctb3_q21=q565
g ctb3_q22=q566
g ctb3_q23=q567
g ctb3_q24=q568

g ctb4_q1=q476
g ctb4_q2=q477
g ctb4_q3=q478
g ctb4_q4=q479
g ctb4_q5=q480
g ctb4_q6=q481
g ctb4_q7=q483
g ctb4_q8=q484
g ctb4_q9=q485
g ctb4_q10=q486
g ctb4_q11=q487
g ctb4_q12=q488
g ctb4_q13=q490
g ctb4_q14=q491
g ctb4_q15=q492
g ctb4_q16=q493
g ctb4_q17=q494
g ctb4_q18=q495
g ctb4_q19=q497
g ctb4_q20=q498
g ctb4_q21=q499
g ctb4_q22=q500
g ctb4_q23=q501
g ctb4_q24=q502

* Keep IDs, treatment variable, and ctb choices
keep subject_id responseid ctb ctb1_q1-ctb1_q24 ctb2_q1-ctb2_q24 ctb3_q1-ctb3_q24 ctb4_q1-ctb4_q24

* Construct numeric variables for ctb options (as we load in string variable data)
foreach x of varlist ctb1_q1-ctb1_q24 ctb2_q1-ctb2_q24 ctb3_q1-ctb3_q24 ctb4_q1-ctb4_q24 {
fre `x', width(100) 
}

foreach x of varlist ctb1_q1-ctb1_q24 ctb2_q1-ctb2_q24 ctb3_q1-ctb3_q24 ctb4_q1-ctb4_q24 {
rename `x' `x'_string
}

* Treatment 1-3 (same options, varying lottery chances/no real payout at all)
foreach t in ctb1 ctb2 ctb3 {
g `t'_q1=1 if `t'_q1_string=="Payment TODAY of $19.00 and payment in 5 WEEKS of $0"
replace `t'_q1=2 if `t'_q1_string=="Payment TODAY of $15.20 and payment in 5 WEEKS of $4.00"
replace `t'_q1=3 if `t'_q1_string=="Payment TODAY of $11.40 and payment in 5 WEEKS of $8.00"
replace `t'_q1=4 if `t'_q1_string=="Payment TODAY of $7.60 and payment in 5 WEEKS of $12.00"
replace `t'_q1=5 if `t'_q1_string=="Payment TODAY of $3.80 and payment in 5 WEEKS of $16.00"
replace `t'_q1=6 if `t'_q1_string=="Payment TODAY of $0 and payment in 5 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q2=1 if `t'_q2_string=="Payment TODAY of $18.00 and payment in 5 WEEKS of $0"
replace `t'_q2=2 if `t'_q2_string=="Payment TODAY of $14.40 and payment in 5 WEEKS of $4.00"
replace `t'_q2=3 if `t'_q2_string=="Payment TODAY of $10.80 and payment in 5 WEEKS of $8.00"
replace `t'_q2=4 if `t'_q2_string=="Payment TODAY of $7.20 and payment in 5 WEEKS of $12.00"
replace `t'_q2=5 if `t'_q2_string=="Payment TODAY of $3.60 and payment in 5 WEEKS of $16.00"
replace `t'_q2=6 if `t'_q2_string=="Payment TODAY of $0 and payment in 5 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q3=1 if `t'_q3_string=="Payment TODAY of $17.00 and payment in 5 WEEKS of $0"
replace `t'_q3=2 if `t'_q3_string=="Payment TODAY of $13.60 and payment in 5 WEEKS of $4.00"
replace `t'_q3=3 if `t'_q3_string=="Payment TODAY of $10.20 and payment in 5 WEEKS of $8.00"
replace `t'_q3=4 if `t'_q3_string=="Payment TODAY of $6.80 and payment in 5 WEEKS of $12.00"
replace `t'_q3=5 if `t'_q3_string=="Payment TODAY of $3.40 and payment in 5 WEEKS of $16.00"
replace `t'_q3=6 if `t'_q3_string=="Payment TODAY of $0 and payment in 5 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q4=1 if `t'_q4_string=="Payment TODAY of $16.00 and payment in 5 WEEKS of $0"
replace `t'_q4=2 if `t'_q4_string=="Payment TODAY of $12.80 and payment in 5 WEEKS of $4.00"
replace `t'_q4=3 if `t'_q4_string=="Payment TODAY of $9.60 and payment in 5 WEEKS of $8.00"
replace `t'_q4=4 if `t'_q4_string=="Payment TODAY of $6.40 and payment in 5 WEEKS of $12.00"
replace `t'_q4=5 if `t'_q4_string=="Payment TODAY of $3.20 and payment in 5 WEEKS of $16.00"
replace `t'_q4=6 if `t'_q4_string=="Payment TODAY of $0 and payment in 5 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q5=1 if `t'_q5_string=="Payment TODAY of $14.00 and payment in 5 WEEKS of $0"
replace `t'_q5=2 if `t'_q5_string=="Payment TODAY of $11.20 and payment in 5 WEEKS of $4.00"
replace `t'_q5=3 if `t'_q5_string=="Payment TODAY of $8.40 and payment in 5 WEEKS of $8.00"
replace `t'_q5=4 if `t'_q5_string=="Payment TODAY of $5.60 and payment in 5 WEEKS of $12.00"
replace `t'_q5=5 if `t'_q5_string=="Payment TODAY of $2.80 and payment in 5 WEEKS of $16.00"
replace `t'_q5=6 if `t'_q5_string=="Payment TODAY of $0 and payment in 5 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q6=1 if `t'_q6_string=="Payment TODAY of $11.00 and payment in 5 WEEKS of $0"
replace `t'_q6=2 if `t'_q6_string=="Payment TODAY of $8.80 and payment in 5 WEEKS of $4.00"
replace `t'_q6=3 if `t'_q6_string=="Payment TODAY of $6.60 and payment in 5 WEEKS of $8.00"
replace `t'_q6=4 if `t'_q6_string=="Payment TODAY of $4.40 and payment in 5 WEEKS of $12.00"
replace `t'_q6=5 if `t'_q6_string=="Payment TODAY of $2.20 and payment in 5 WEEKS of $16.00"
replace `t'_q6=6 if `t'_q6_string=="Payment TODAY of $0 and payment in 5 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q7=1 if `t'_q7_string=="Payment TODAY of $20.00 and payment in 9 WEEKS of $0"
replace `t'_q7=2 if `t'_q7_string=="Payment TODAY of $16.00 and payment in 9 WEEKS of $4.00"
replace `t'_q7=3 if `t'_q7_string=="Payment TODAY of $12.00 and payment in 9 WEEKS of $8.00"
replace `t'_q7=4 if `t'_q7_string=="Payment TODAY of $8.00 and payment in 9 WEEKS of $12.00"
replace `t'_q7=5 if `t'_q7_string=="Payment TODAY of $4.00 and payment in 9 WEEKS of $16.00"
replace `t'_q7=6 if `t'_q7_string=="Payment TODAY of $0 and payment in 9 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q8=1 if `t'_q8_string=="Payment TODAY of $19.00 and payment in 9 WEEKS of $0"
replace `t'_q8=2 if `t'_q8_string=="Payment TODAY of $15.20 and payment in 9 WEEKS of $4.00"
replace `t'_q8=3 if `t'_q8_string=="Payment TODAY of $11.40 and payment in 9 WEEKS of $8.00"
replace `t'_q8=4 if `t'_q8_string=="Payment TODAY of $7.60 and payment in 9 WEEKS of $12.00"
replace `t'_q8=5 if `t'_q8_string=="Payment TODAY of $3.80 and payment in 9 WEEKS of $16.00"
replace `t'_q8=6 if `t'_q8_string=="Payment TODAY of $0 and payment in 9 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q9=1 if `t'_q9_string=="Payment TODAY of $17.00 and payment in 9 WEEKS of $0"
replace `t'_q9=2 if `t'_q9_string=="Payment TODAY of $14.40 and payment in 9 WEEKS of $4.00"
replace `t'_q9=3 if `t'_q9_string=="Payment TODAY of $10.80 and payment in 9 WEEKS of $8.00"
replace `t'_q9=4 if `t'_q9_string=="Payment TODAY of $7.20 and payment in 9 WEEKS of $12.00"
replace `t'_q9=5 if `t'_q9_string=="Payment TODAY of $3.60 and payment in 9 WEEKS of $16.00"
replace `t'_q9=6 if `t'_q9_string=="Payment TODAY of $0 and payment in 9 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q10=1 if `t'_q10_string=="Payment TODAY of $15.00 and payment in 9 WEEKS of $0"
replace `t'_q10=2 if `t'_q10_string=="Payment TODAY of $12.00 and payment in 9 WEEKS of $4.00"
replace `t'_q10=3 if `t'_q10_string=="Payment TODAY of $9.00 and payment in 9 WEEKS of $8.00"
replace `t'_q10=4 if `t'_q10_string=="Payment TODAY of $6.00 and payment in 9 WEEKS of $12.00"
replace `t'_q10=5 if `t'_q10_string=="Payment TODAY of $3.00 and payment in 9 WEEKS of $16.00"
replace `t'_q10=6 if `t'_q10_string=="Payment TODAY of $0 and payment in 9 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q11=1 if `t'_q11_string=="Payment TODAY of $12.00 and payment in 9 WEEKS of $0"
replace `t'_q11=2 if `t'_q11_string=="Payment TODAY of $9.60 and payment in 9 WEEKS of $4.00"
replace `t'_q11=3 if `t'_q11_string=="Payment TODAY of $7.20 and payment in 9 WEEKS of $8.00"
replace `t'_q11=4 if `t'_q11_string=="Payment TODAY of $4.80 and payment in 9 WEEKS of $12.00"
replace `t'_q11=5 if `t'_q11_string=="Payment TODAY of $2.40 and payment in 9 WEEKS of $16.00"
replace `t'_q11=6 if `t'_q11_string=="Payment TODAY of $0 and payment in 9 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q12=1 if `t'_q12_string=="Payment TODAY of $9.00 and payment in 9 WEEKS of $0"
replace `t'_q12=2 if `t'_q12_string=="Payment TODAY of $7.20 and payment in 9 WEEKS of $4.00"
replace `t'_q12=3 if `t'_q12_string=="Payment TODAY of $5.40 and payment in 9 WEEKS of $8.00"
replace `t'_q12=4 if `t'_q12_string=="Payment TODAY of $3.60 and payment in 9 WEEKS of $12.00"
replace `t'_q12=5 if `t'_q12_string=="Payment TODAY of $1.80 and payment in 9 WEEKS of $16.00"
replace `t'_q12=6 if `t'_q12_string=="Payment TODAY of $0 and payment in 9 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q13=1 if `t'_q13_string=="Payment in 5 WEEKS of $19.00 and payment in 10 WEEKS of $0"
replace `t'_q13=2 if `t'_q13_string=="Payment in 5 WEEKS of $15.20 and payment in 10 WEEKS of $4.00"
replace `t'_q13=3 if `t'_q13_string=="Payment in 5 WEEKS of $11.40 and payment in 10 WEEKS of $8.00"
replace `t'_q13=4 if `t'_q13_string=="Payment in 5 WEEKS of $7.60 and payment in 10 WEEKS of $12.00"
replace `t'_q13=5 if `t'_q13_string=="Payment in 5 WEEKS of $3.80 and payment in 10 WEEKS of $16.00"
replace `t'_q13=6 if `t'_q13_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q14=1 if `t'_q14_string=="Payment in 5 WEEKS of $18.00 and payment in 10 WEEKS of $0"
replace `t'_q14=2 if `t'_q14_string=="Payment in 5 WEEKS of $14.40 and payment in 10 WEEKS of $4.00"
replace `t'_q14=3 if `t'_q14_string=="Payment in 5 WEEKS of $10.80 and payment in 10 WEEKS of $8.0"
replace `t'_q14=4 if `t'_q14_string=="Payment in 5 WEEKS of $7.20 and payment in 10 WEEKS of $12.00"
replace `t'_q14=5 if `t'_q14_string=="Payment in 5 WEEKS of $3.60 and payment in 10 WEEKS of $16.00"
replace `t'_q14=6 if `t'_q14_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q15=1 if `t'_q15_string=="Payment in 5 WEEKS of $17.00 and payment in 10 WEEKS of $0"
replace `t'_q15=2 if `t'_q15_string=="Payment in 5 WEEKS of $13.60 and payment in 10 WEEKS of $4.00"
replace `t'_q15=3 if `t'_q15_string=="Payment in 5 WEEKS of $10.20 and payment in 10 WEEKS of $8.00"
replace `t'_q15=4 if `t'_q15_string=="Payment in 5 WEEKS of $6.80 and payment in 10 WEEKS of $12.00"
replace `t'_q15=5 if `t'_q15_string=="Payment in 5 WEEKS of $3.40 and payment in 10 WEEKS of $16.00"
replace `t'_q15=6 if `t'_q15_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q16=1 if `t'_q16_string=="Payment in 5 WEEKS of $16.00 and payment in 10 WEEKS of $0"
replace `t'_q16=2 if `t'_q16_string=="Payment in 5 WEEKS of $12.80 and payment in 10 WEEKS of $4.00"
replace `t'_q16=3 if `t'_q16_string=="Payment in 5 WEEKS of $9.60 and payment in 10 WEEKS of $8.00"
replace `t'_q16=4 if `t'_q16_string=="Payment in 5 WEEKS of $6.40 and payment in 10 WEEKS of $12.00"
replace `t'_q16=5 if `t'_q16_string=="Payment in 5 WEEKS of $3.20 and payment in 10 WEEKS of $16.00"
replace `t'_q16=6 if `t'_q16_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q17=1 if `t'_q17_string=="Payment in 5 WEEKS of $14.00 and payment in 10 WEEKS of $0"
replace `t'_q17=2 if `t'_q17_string=="Payment in 5 WEEKS of $11.20 and payment in 10 WEEKS of $4.00"
replace `t'_q17=3 if `t'_q17_string=="Payment in 5 WEEKS of $8.40 and payment in 10 WEEKS of $8.00"
replace `t'_q17=4 if `t'_q17_string=="Payment in 5 WEEKS of $5.60 and payment in 10 WEEKS of $12.00"
replace `t'_q17=5 if `t'_q17_string=="Payment in 5 WEEKS of $2.80 and payment in 10 WEEKS of $16.00"
replace `t'_q17=6 if `t'_q17_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q18=1 if `t'_q18_string=="Payment in 5 WEEKS of $11.00 and payment in 10 WEEKS of $0"
replace `t'_q18=2 if `t'_q18_string=="Payment in 5 WEEKS of $8.80 and payment in 10 WEEKS of $4.00"
replace `t'_q18=3 if `t'_q18_string=="Payment in 5 WEEKS of $6.60 and payment in 10 WEEKS of $8.00"
replace `t'_q18=4 if `t'_q18_string=="Payment in 5 WEEKS of $4.40 and payment in 10 WEEKS of $12.00"
replace `t'_q18=5 if `t'_q18_string=="Payment in 5 WEEKS of $2.20 and payment in 10 WEEKS of $16.00"
replace `t'_q18=6 if `t'_q18_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q19=1 if `t'_q19_string=="Payment in 5 WEEKS of $20.00 and payment in 14 WEEKS of $0"
replace `t'_q19=2 if `t'_q19_string=="Payment in 5 WEEKS of $16.00 and payment in 14 WEEKS of $4.00"
replace `t'_q19=3 if `t'_q19_string=="Payment in 5 WEEKS of $12.00 and payment in 14 WEEKS of $8.00"
replace `t'_q19=4 if `t'_q19_string=="Payment in 5 WEEKS of $8.00 and payment in 14 WEEKS of $12.00"
replace `t'_q19=5 if `t'_q19_string=="Payment in 5 WEEKS of $4.00 and payment in 14 WEEKS of $16.00"
replace `t'_q19=6 if `t'_q19_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q20=1 if `t'_q20_string=="Payment in 5 WEEKS of $19.00 and payment in 14 WEEKS of $0"
replace `t'_q20=2 if `t'_q20_string=="Payment in 5 WEEKS of $15.20 and payment in 14 WEEKS of $4.00"
replace `t'_q20=3 if `t'_q20_string=="Payment in 5 WEEKS of $11.40 and payment in 14 WEEKS of $8.00"
replace `t'_q20=4 if `t'_q20_string=="Payment in 5 WEEKS of $7.60 and payment in 14 WEEKS of $12.00"
replace `t'_q20=5 if `t'_q20_string=="Payment in 5 WEEKS of $3.80 and payment in 14 WEEKS of $16.00"
replace `t'_q20=6 if `t'_q20_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q21=1 if `t'_q21_string=="Payment in 5 WEEKS of $17.00 and payment in 14 WEEKS of $0"
replace `t'_q21=2 if `t'_q21_string=="Payment in 5 WEEKS of $14.40 and payment in 14 WEEKS of $4.00"
replace `t'_q21=3 if `t'_q21_string=="Payment in 5 WEEKS of $10.80 and payment in 14 WEEKS of $8.00"
replace `t'_q21=4 if `t'_q21_string=="Payment in 5 WEEKS of $7.20 and payment in 14 WEEKS of $12.00"
replace `t'_q21=5 if `t'_q21_string=="Payment in 5 WEEKS of $3.60 and payment in 14 WEEKS of $16.00"
replace `t'_q21=6 if `t'_q21_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q22=1 if `t'_q22_string=="Payment in 5 WEEKS of $15.00 and payment in 14 WEEKS of $0"
replace `t'_q22=2 if `t'_q22_string=="Payment in 5 WEEKS of $12.00 and payment in 14 WEEKS of $4.00"
replace `t'_q22=3 if `t'_q22_string=="Payment in 5 WEEKS of $9.00 and payment in 14 WEEKS of $8.00"
replace `t'_q22=4 if `t'_q22_string=="Payment in 5 WEEKS of $6.00 and payment in 14 WEEKS of $12.00"
replace `t'_q22=5 if `t'_q22_string=="Payment in 5 WEEKS of $3.00 and payment in 14 WEEKS of $16.00"
replace `t'_q22=6 if `t'_q22_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q23=1 if `t'_q23_string=="Payment in 5 WEEKS of $12.00 and payment in 14 WEEKS of $0"
replace `t'_q23=2 if `t'_q23_string=="Payment in 5 WEEKS of $9.60 and payment in 14 WEEKS of $4.00"
replace `t'_q23=3 if `t'_q23_string=="Payment in 5 WEEKS of $7.20 and payment in 14 WEEKS of $8.00"
replace `t'_q23=4 if `t'_q23_string=="Payment in 5 WEEKS of $4.80 and payment in 14 WEEKS of $12.00"
replace `t'_q23=5 if `t'_q23_string=="Payment in 5 WEEKS of $2.40 and payment in 14 WEEKS of $16.00"
replace `t'_q23=6 if `t'_q23_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $20.00"
}

foreach t in ctb1 ctb2 ctb3 {
g `t'_q24=1 if `t'_q24_string=="Payment in 5 WEEKS of $9.00 and payment in 14 WEEKS of $0"
replace `t'_q24=2 if `t'_q24_string=="Payment in 5 WEEKS of $7.20 and payment in 14 WEEKS of $4.00"
replace `t'_q24=3 if `t'_q24_string=="Payment in 5 WEEKS of $5.40 and payment in 14 WEEKS of $8.00"
replace `t'_q24=4 if `t'_q24_string=="Payment in 5 WEEKS of $3.60 and payment in 14 WEEKS of $12.00"
replace `t'_q24=5 if `t'_q24_string=="Payment in 5 WEEKS of $1.80 and payment in 14 WEEKS of $16.00"
replace `t'_q24=6 if `t'_q24_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $20.00"
}


* Treatment 4 (hypothetical high payoffs)
foreach t in ctb4 {
g `t'_q1=1 if `t'_q1_string=="Payment TODAY of $1,900.00 and payment in 5 WEEKS of $0"
replace `t'_q1=2 if `t'_q1_string=="Payment TODAY of $1,520.00 and payment in 5 WEEKS of $400.00"
replace `t'_q1=3 if `t'_q1_string=="Payment TODAY of $1,140.00 and payment in 5 WEEKS of $800.00"
replace `t'_q1=4 if `t'_q1_string=="Payment TODAY of $760.00 and payment in 5 WEEKS of $1,200.00"
replace `t'_q1=5 if `t'_q1_string=="Payment TODAY of $380.00 and payment in 5 WEEKS of $1,600.00"
replace `t'_q1=6 if `t'_q1_string=="Payment TODAY of $0 and payment in 5 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q2=1 if `t'_q2_string=="Payment TODAY of $1,800.00 and payment in 5 WEEKS of $0"
replace `t'_q2=2 if `t'_q2_string=="Payment TODAY of $1,440.00 and payment in 5 WEEKS of $400.00"
replace `t'_q2=3 if `t'_q2_string=="Payment TODAY of $1,080.00 and payment in 5 WEEKS of $800.00"
replace `t'_q2=4 if `t'_q2_string=="Payment TODAY of $720.00 and payment in 5 WEEKS of $1,200.00"
replace `t'_q2=5 if `t'_q2_string=="Payment TODAY of $360.00 and payment in 5 WEEKS of $1,600.00"
replace `t'_q2=6 if `t'_q2_string=="Payment TODAY of $0 and payment in 5 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q3=1 if `t'_q3_string=="Payment TODAY of $1,700.00 and payment in 5 WEEKS of $0"
replace `t'_q3=2 if `t'_q3_string=="Payment TODAY of $1,360.00 and payment in 5 WEEKS of $400.00"
replace `t'_q3=3 if `t'_q3_string=="Payment TODAY of $1,020.00 and payment in 5 WEEKS of $800.00"
replace `t'_q3=4 if `t'_q3_string=="Payment TODAY of $680.00 and payment in 5 WEEKS of $1,200.00"
replace `t'_q3=5 if `t'_q3_string=="Payment TODAY of $340.00 and payment in 5 WEEKS of $1,600.00"
replace `t'_q3=6 if `t'_q3_string=="Payment TODAY of $0 and payment in 5 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q4=1 if `t'_q4_string=="Payment TODAY of $1,600.00 and payment in 5 WEEKS of $0"
replace `t'_q4=2 if `t'_q4_string=="Payment TODAY of $1,280.00 and payment in 5 WEEKS of $400.00"
replace `t'_q4=3 if `t'_q4_string=="Payment TODAY of $960.00 and payment in 5 WEEKS of $800.00"
replace `t'_q4=4 if `t'_q4_string=="Payment TODAY of $640.00 and payment in 5 WEEKS of $1,200.00"
replace `t'_q4=5 if `t'_q4_string=="Payment TODAY of $320.00 and payment in 5 WEEKS of $1,600.00"
replace `t'_q4=6 if `t'_q4_string=="Payment TODAY of $0 and payment in 5 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q5=1 if `t'_q5_string=="Payment TODAY of $1,400.00 and payment in 5 WEEKS of $0"
replace `t'_q5=2 if `t'_q5_string=="Payment TODAY of $1,120.00 and payment in 5 WEEKS of $400.00"
replace `t'_q5=3 if `t'_q5_string=="Payment TODAY of $840.00 and payment in 5 WEEKS of $800.00"
replace `t'_q5=4 if `t'_q5_string=="Payment TODAY of $560.00 and payment in 5 WEEKS of $1,200.00"
replace `t'_q5=5 if `t'_q5_string=="Payment TODAY of $280.00 and payment in 5 WEEKS of $1,600.00"
replace `t'_q5=6 if `t'_q5_string=="Payment TODAY of $0 and payment in 5 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q6=1 if `t'_q6_string=="Payment TODAY of $1,100.00 and payment in 5 WEEKS of $0"
replace `t'_q6=2 if `t'_q6_string=="Payment TODAY of $880.00 and payment in 5 WEEKS of $400.00"
replace `t'_q6=3 if `t'_q6_string=="Payment TODAY of $660.00 and payment in 5 WEEKS of $800.00"
replace `t'_q6=4 if `t'_q6_string=="Payment TODAY of $440.00 and payment in 5 WEEKS of $1,200.00"
replace `t'_q6=5 if `t'_q6_string=="Payment TODAY of $220.00 and payment in 5 WEEKS of $1,600.00"
replace `t'_q6=6 if `t'_q6_string=="Payment TODAY of $0 and payment in 5 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q7=1 if `t'_q7_string=="Payment TODAY of $2,000.00 and payment in 9 WEEKS of $0"
replace `t'_q7=2 if `t'_q7_string=="Payment TODAY of $1,600.00 and payment in 9 WEEKS of $400.00"
replace `t'_q7=3 if `t'_q7_string=="Payment TODAY of $1,200.00 and payment in 9 WEEKS of $800.00"
replace `t'_q7=4 if `t'_q7_string=="Payment TODAY of $800.00 and payment in 9 WEEKS of $1,200.00"
replace `t'_q7=5 if `t'_q7_string=="Payment TODAY of $400.00 and payment in 9 WEEKS of $1,600.00"
replace `t'_q7=6 if `t'_q7_string=="Payment TODAY of $0 and payment in 9 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q8=1 if `t'_q8_string=="Payment TODAY of $1,900.00 and payment in 9 WEEKS of $0"
replace `t'_q8=2 if `t'_q8_string=="Payment TODAY of $1,520.00 and payment in 9 WEEKS of $400.00"
replace `t'_q8=3 if `t'_q8_string=="Payment TODAY of $1,140.00 and payment in 9 WEEKS of $800.00"
replace `t'_q8=4 if `t'_q8_string=="Payment TODAY of $760.00 and payment in 9 WEEKS of $1,200.00"
replace `t'_q8=5 if `t'_q8_string=="Payment TODAY of $380.00 and payment in 9 WEEKS of $1,600.00"
replace `t'_q8=6 if `t'_q8_string=="Payment TODAY of $0 and payment in 9 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q9=1 if `t'_q9_string=="Payment TODAY of $1,700.00 and payment in 9 WEEKS of $0"
replace `t'_q9=2 if `t'_q9_string=="Payment TODAY of $1,440.00 and payment in 9 WEEKS of $400.00"
replace `t'_q9=3 if `t'_q9_string=="Payment TODAY of $1,080.00 and payment in 9 WEEKS of $800.00"
replace `t'_q9=4 if `t'_q9_string=="Payment TODAY of $720.00 and payment in 9 WEEKS of $1,200.00"
replace `t'_q9=5 if `t'_q9_string=="Payment TODAY of $360.00 and payment in 9 WEEKS of $1,600.00"
replace `t'_q9=6 if `t'_q9_string=="Payment TODAY of $0 and payment in 9 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q10=1 if `t'_q10_string=="Payment TODAY of $1,500.00 and payment in 9 WEEKS of $0"
replace `t'_q10=2 if `t'_q10_string=="Payment TODAY of $1,200.00 and payment in 9 WEEKS of $400.00"
replace `t'_q10=3 if `t'_q10_string=="Payment TODAY of $900.00 and payment in 9 WEEKS of $800.00"
replace `t'_q10=4 if `t'_q10_string=="Payment TODAY of $600.00 and payment in 9 WEEKS of $1,200.00"
replace `t'_q10=5 if `t'_q10_string=="Payment TODAY of $300.00 and payment in 9 WEEKS of $1,600.00"
replace `t'_q10=6 if `t'_q10_string=="Payment TODAY of $0 and payment in 9 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q11=1 if `t'_q11_string=="Payment TODAY of $1,200.00 and payment in 9 WEEKS of $0"
replace `t'_q11=2 if `t'_q11_string=="Payment TODAY of $960.00 and payment in 9 WEEKS of $400.00"
replace `t'_q11=3 if `t'_q11_string=="Payment TODAY of $720.00 and payment in 9 WEEKS of $800.00"
replace `t'_q11=4 if `t'_q11_string=="Payment TODAY of $480.00 and payment in 9 WEEKS of $1,200.00"
replace `t'_q11=5 if `t'_q11_string=="Payment TODAY of $240.00 and payment in 9 WEEKS of $1,600.00"
replace `t'_q11=6 if `t'_q11_string=="Payment TODAY of $0 and payment in 9 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q12=1 if `t'_q12_string=="Payment TODAY of $900.00 and payment in 9 WEEKS of $0"
replace `t'_q12=2 if `t'_q12_string=="Payment TODAY of $720.00 and payment in 9 WEEKS of $400.00"
replace `t'_q12=3 if `t'_q12_string=="Payment TODAY of $540.00 and payment in 9 WEEKS of $800.00"
replace `t'_q12=4 if `t'_q12_string=="Payment TODAY of $360.00 and payment in 9 WEEKS of $1,200.00"
replace `t'_q12=5 if `t'_q12_string=="Payment TODAY of $180.00 and payment in 9 WEEKS of $1,600.00"
replace `t'_q12=6 if `t'_q12_string=="Payment TODAY of $0 and payment in 9 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q13=1 if `t'_q13_string=="Payment in 5 WEEKS of $1,900.00 and payment in 10 WEEKS of $0"
replace `t'_q13=2 if `t'_q13_string=="Payment in 5 WEEKS of $1,520.00 and payment in 10 WEEKS of $400.00"
replace `t'_q13=3 if `t'_q13_string=="Payment in 5 WEEKS of $1,140.00 and payment in 10 WEEKS of $800.00"
replace `t'_q13=4 if `t'_q13_string=="Payment in 5 WEEKS of $760.00 and payment in 10 WEEKS of $1,200.00"
replace `t'_q13=5 if `t'_q13_string=="Payment in 5 WEEKS of $380.00 and payment in 10 WEEKS of $1,600.00"
replace `t'_q13=6 if `t'_q13_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q14=1 if `t'_q14_string=="Payment in 5 WEEKS of $1,800.00 and payment in 10 WEEKS of $0"
replace `t'_q14=2 if `t'_q14_string=="Payment in 5 WEEKS of $1,440.00 and payment in 10 WEEKS of $400.00"
replace `t'_q14=3 if `t'_q14_string=="Payment in 5 WEEKS of $1,080.00 and payment in 10 WEEKS of $800.00"
replace `t'_q14=4 if `t'_q14_string=="Payment in 5 WEEKS of $720.00 and payment in 10 WEEKS of $1,200.00"
replace `t'_q14=5 if `t'_q14_string=="Payment in 5 WEEKS of $360.00 and payment in 10 WEEKS of $1,600.00"
replace `t'_q14=6 if `t'_q14_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q15=1 if `t'_q15_string=="Payment in 5 WEEKS of $1,700.00 and payment in 10 WEEKS of $0"
replace `t'_q15=2 if `t'_q15_string=="Payment in 5 WEEKS of $1,360.00 and payment in 10 WEEKS of $400.00"
replace `t'_q15=3 if `t'_q15_string=="Payment in 5 WEEKS of $1,020.00 and payment in 10 WEEKS of $800.00"
replace `t'_q15=4 if `t'_q15_string=="Payment in 5 WEEKS of $680.00 and payment in 10 WEEKS of $1,200.00"
replace `t'_q15=5 if `t'_q15_string=="Payment in 5 WEEKS of $340.00 and payment in 10 WEEKS of $1,600.00"
replace `t'_q15=6 if `t'_q15_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q16=1 if `t'_q16_string=="Payment in 5 WEEKS of $1,600.00 and payment in 10 WEEKS of $0"
replace `t'_q16=2 if `t'_q16_string=="Payment in 5 WEEKS of $1,280.00 and payment in 10 WEEKS of $400.00"
replace `t'_q16=3 if `t'_q16_string=="Payment in 5 WEEKS of $960.00 and payment in 10 WEEKS of $800.00"
replace `t'_q16=4 if `t'_q16_string=="Payment in 5 WEEKS of $640.00 and payment in 10 WEEKS of $1,200.00"
replace `t'_q16=5 if `t'_q16_string=="Payment in 5 WEEKS of $320.00 and payment in 10 WEEKS of $1,600.00"
replace `t'_q16=6 if `t'_q16_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q17=1 if `t'_q17_string=="Payment in 5 WEEKS of $1,400.00 and payment in 10 WEEKS of $0"
replace `t'_q17=2 if `t'_q17_string=="Payment in 5 WEEKS of $1,120.00 and payment in 10 WEEKS of $400.00"
replace `t'_q17=3 if `t'_q17_string=="Payment in 5 WEEKS of $840.00 and payment in 10 WEEKS of $800.00"
replace `t'_q17=4 if `t'_q17_string=="Payment in 5 WEEKS of $560.00 and payment in 10 WEEKS of $1,200.00"
replace `t'_q17=5 if `t'_q17_string=="Payment in 5 WEEKS of $280.00 and payment in 10 WEEKS of $1,600.00"
replace `t'_q17=6 if `t'_q17_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q18=1 if `t'_q18_string=="Payment in 5 WEEKS of $1,100.00 and payment in 10 WEEKS of $0"
replace `t'_q18=2 if `t'_q18_string=="Payment in 5 WEEKS of $880.00 and payment in 10 WEEKS of $400.00"
replace `t'_q18=3 if `t'_q18_string=="Payment in 5 WEEKS of $660.00 and payment in 10 WEEKS of $800.00"
replace `t'_q18=4 if `t'_q18_string=="Payment in 5 WEEKS of $440.00 and payment in 10 WEEKS of $1,200.00"
replace `t'_q18=5 if `t'_q18_string=="Payment in 5 WEEKS of $220.00 and payment in 10 WEEKS of $1,600.00"
replace `t'_q18=6 if `t'_q18_string=="Payment in 5 WEEKS of $0 and payment in 10 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q19=1 if `t'_q19_string=="Payment in 5 WEEKS of $2,000.00 and payment in 14 WEEKS of $0"
replace `t'_q19=2 if `t'_q19_string=="Payment in 5 WEEKS of $1,600.00 and payment in 14 WEEKS of $400.00"
replace `t'_q19=3 if `t'_q19_string=="Payment in 5 WEEKS of $1,200.00 and payment in 14 WEEKS of $800.00"
replace `t'_q19=4 if `t'_q19_string=="Payment in 5 WEEKS of $800.00 and payment in 14 WEEKS of $1,200.00"
replace `t'_q19=5 if `t'_q19_string=="Payment in 5 WEEKS of $400.00 and payment in 14 WEEKS of $1,600.00"
replace `t'_q19=6 if `t'_q19_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q20=1 if `t'_q20_string=="Payment in 5 WEEKS of $1,900.00 and payment in 14 WEEKS of $0"
replace `t'_q20=2 if `t'_q20_string=="Payment in 5 WEEKS of $1,520.00 and payment in 14 WEEKS of $400.00"
replace `t'_q20=3 if `t'_q20_string=="Payment in 5 WEEKS of $1,140.00 and payment in 14 WEEKS of $800.00"
replace `t'_q20=4 if `t'_q20_string=="Payment in 5 WEEKS of $760.00 and payment in 14 WEEKS of $1,200.00"
replace `t'_q20=5 if `t'_q20_string=="Payment in 5 WEEKS of $380.00 and payment in 14 WEEKS of $1,600.00"
replace `t'_q20=6 if `t'_q20_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q21=1 if `t'_q21_string=="Payment in 5 WEEKS of $1,700.00 and payment in 14 WEEKS of $0"
replace `t'_q21=2 if `t'_q21_string=="Payment in 5 WEEKS of $1,440.00 and payment in 14 WEEKS of $400.00"
replace `t'_q21=3 if `t'_q21_string=="Payment in 5 WEEKS of $1,080.00 and payment in 14 WEEKS of $800.00"
replace `t'_q21=4 if `t'_q21_string=="Payment in 5 WEEKS of $720.00 and payment in 14 WEEKS of $1,200.00"
replace `t'_q21=5 if `t'_q21_string=="Payment in 5 WEEKS of $360.00 and payment in 14 WEEKS of $1,600.00"
replace `t'_q21=6 if `t'_q21_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q22=1 if `t'_q22_string=="Payment in 5 WEEKS of $1,500.00 and payment in 14 WEEKS of $0"
replace `t'_q22=2 if `t'_q22_string=="Payment in 5 WEEKS of $1,200.00 and payment in 14 WEEKS of $400.00"
replace `t'_q22=3 if `t'_q22_string=="Payment in 5 WEEKS of $900.00 and payment in 14 WEEKS of $800.00"
replace `t'_q22=4 if `t'_q22_string=="Payment in 5 WEEKS of $600.00 and payment in 14 WEEKS of $1,200.00"
replace `t'_q22=5 if `t'_q22_string=="Payment in 5 WEEKS of $300.00 and payment in 14 WEEKS of $1,600.00"
replace `t'_q22=6 if `t'_q22_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q23=1 if `t'_q23_string=="Payment in 5 WEEKS of $1,200.00 and payment in 14 WEEKS of $0"
replace `t'_q23=2 if `t'_q23_string=="Payment in 5 WEEKS of $960.00 and payment in 14 WEEKS of $400.00"
replace `t'_q23=3 if `t'_q23_string=="Payment in 5 WEEKS of $720.00 and payment in 14 WEEKS of $800.00"
replace `t'_q23=4 if `t'_q23_string=="Payment in 5 WEEKS of $480.00 and payment in 14 WEEKS of $1,200.00"
replace `t'_q23=5 if `t'_q23_string=="Payment in 5 WEEKS of $240.00 and payment in 14 WEEKS of $1,600.00"
replace `t'_q23=6 if `t'_q23_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $2,000.00"
}

foreach t in ctb4 {
g `t'_q24=1 if `t'_q24_string=="Payment in 5 WEEKS of $900.00 and payment in 14 WEEKS of $0"
replace `t'_q24=2 if `t'_q24_string=="Payment in 5 WEEKS of $720.00 and payment in 14 WEEKS of $400.00"
replace `t'_q24=3 if `t'_q24_string=="Payment in 5 WEEKS of $540.00 and payment in 14 WEEKS of $800.00"
replace `t'_q24=4 if `t'_q24_string=="Payment in 5 WEEKS of $360.00 and payment in 14 WEEKS of $1.200.00"
replace `t'_q24=5 if `t'_q24_string=="Payment in 5 WEEKS of $180.00 and payment in 14 WEEKS of $1,600.00"
replace `t'_q24=6 if `t'_q24_string=="Payment in 5 WEEKS of $0 and payment in 14 WEEKS of $2,000.00"
}


* Sum across treatment group ctb choices to get one ctb choice per row (per respondent) (i.e. for each respondent this represents their ctb choice given the treatment they got)
forval i=1/24 {
egen c`i'=rowtotal(ctb1_q`i' ctb2_q`i' ctb3_q`i' ctb4_q`i')
}

forval i=1/24 {
tab c`i'
}

keep subject_id responseid ctb c1-c24 

* Reshape to data structure with 24 observations for each respondent, such that each row is a unique respondent-choice observations (the survey has 24 ctb choice questions)
reshape long c, i(subject_id) j(budget_number)

* Save ctbdata, replace
save "$data/ctbdata", replace

*** Get payoff information
* Instrument details (from Andreoni et al. 2015)
clear
insheet using "$data/instrument_details.txt"
sort budget_number
save "$data/instrument_details", replace
clear

use "$data/ctbdata", clear
sort budget_number
merge m:1 budget_number using "$data/instrument_details"
drop _merge

* Choice variables
gen t0=.
replace t0=1 if sooner_date==0
replace t0=0 if sooner_date!=0 & sooner_date!= .
tab t0

gen k=.
replace k=delay_weeks*7
tab k

* Sooner and later endowments to determine values for each option
* and compute the price ratio
*  (from Andreoni et al. 2015)
order endowment_soon endowment_later
gen pratio=endowment_later/endowment_soon
tab pratio

* Define values for each option (from Andreoni et al. 2015)
forvalues i=1(1)5 {
gen soon_`i'=20/pratio-(`i'-1)*(20/pratio)/(5)
}
gen soon_6=0

forvalues i = 1(1)6 {
gen late_`i'=(`i'-1)*4
}

* Associate choice options with values
gen sooner_choice=.
forvalues i=1(1)6 {
replace sooner_choice = soon_`i' if c==`i'
}

* Keep respondents even if no variation in chosen option across CTB questions
codebook subject_id // 5,820 respondents
tab c
egen choice_sd=sd(c), by(subject_id)
*drop if choice_sd==0
codebook subject_id //

* If choice is always 1 (this means respondent always
* chose payment today) 
* if always 6 this means always the latest payment was chosen
tab c if choice_sd==0

* Indicator for always soonest
* No variation and choice condition
gen cbt_alw_soonest=0
replace cbt_alw_soonest=1 if choice_sd==0 & c==1

* Indicator for always latest
* No variation and choice condition
gen cbt_alw_latest=0
replace cbt_alw_latest=1 if choice_sd==0 & c==6

* Chosen sooner and later payouts for estimation
* Payouts
g payout_soon=.
forval i=1(1)6 {
replace payout_soon=soon_`i' if c==`i'
}

g payout_late=.
forval i=1(1)6 {
replace payout_late=late_`i' if c==`i'
}

* Follow Andreoni et al.'s replication code
* and replace 0 with .001 (although JEBO article says
* they replaced with .01)
g payout_soon_adjusted=payout_soon
replace payout_soon_adjusted=0.001 if payout_soon==0
g payout_late_adjusted=payout_late
replace payout_late_adjusted=0.001 if payout_late==0

g allocation_ratio_adjusted=(payout_soon_adjusted/payout_late_adjusted)
g log_allocation_ratio=ln(allocation_ratio_adjusted)

* This is the dependent variable ln(x_star/y_star) in the Andreoni code:
gen ln_consrat_star=log_allocation_ratio

* Log price ratio
g ln_pratio=ln(pratio)


*** Individual level CTB estimation
* Prepare vars for estimates and SEs
gen a_ind_ctb = .
gen b_ind_ctb = .
gen d_ind_ctb = .

label var a_ind_ctb "Estimate: Risk Aversion: Alpha"
label var b_ind_ctb "Estimate: Present Bias: Beta"
label var d_ind_ctb "Estimate: Long-run Discounting: Delta"

gen a_ind_ctb_se = .
gen b_ind_ctb_se = .
gen d_ind_ctb_se = .

label var a_ind_ctb_se "Standard Error: Alpha"
label var b_ind_ctb_se "Standard Error: Beta"
label var d_ind_ctb_se "Standard Error: Delta"
*label var r_ind_ctb_se "Standar Error: Annualized Discount Rate"

* Generate IDs within each treatment group (now multiple obs per
* respondent)
forvalues i=1(1)4 {
egen id_ctb_`i' = group(subject_id) if ctb==`i'
}
gen id_group=id_ctb_1 
replace id_group=id_ctb_2 if id_group==.
replace id_group=id_ctb_3 if id_group==.
replace id_group=id_ctb_4 if id_group==.

egen id = group(subject_id)
tab choice_sd if choice_sd==0

* Save ctb data
sort subject_id budget_number
compress

save "$data/ctbdata_estimation", replace
clear
